﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DevelopEngine;

public class UIName
{ //UI命名不要有特殊符号
	public const string Win = "PanelWin";
	public const string GameOver = "PanelGameOver";
	public const string Pause = "PanelPause";
	public const string GamePause = "PanelPauseGame";
	//Muen菜单
	public const string Map = "Map";
	public const string Return = "Return";
	public const string Customs1 = "Customs1";
	public const string Customs2 = "Customs3";
	public const string Customs3 = "Customs3";
	
	
}
public class UIManager : MonoSingleton<UIManager> {
	
	private Dictionary<string ,UIScene> mUIScene = new Dictionary<string, UIScene>();


   

    public void InitializeUIs()
	{
		
		mUIScene.Clear();
		
		UIScene[] uis =GameObject.FindObjectsOfType<UIScene>();
		if (uis != null)
		{
			foreach (var ui in uis)
			{
				
				ui.SetVisible(false);
				mUIScene.Add(ui.gameObject.name, ui);
			}
		}
	}

	public void SetVisible (string name, bool visible)
	{
		if (visible && !IsVisible(name))
		{
			OpenScene(name);
		}
		else if (!visible && IsVisible(name))
		{
			CloseScene(name);
		}
	}

	public bool IsVisible (string name)
	{
		UIScene ui = GetUI(name);
		if (ui != null)
			return ui.IsVisible();		
		return false;
	}
	private UIScene GetUI(string name)
	{
		UIScene ui;
		return mUIScene.TryGetValue(name , out ui) ? ui : null;
	}

	public T GetUI<T> (string name) where T : UIScene
	{
		return GetUI(name) as T;
	}

	private bool isLoaded(string name)
	{
		if (mUIScene.ContainsKey(name))
		{
			return true;
		}
		return false;
	}

	private void OpenScene(string name)
	{
		if (isLoaded(name))
		{
			mUIScene[name].SetVisible(true);
		}
	}
	private void CloseScene(string name)
	{
		if (isLoaded(name))
		{
			mUIScene[name].SetVisible(false);
		}
	}

	
}
